﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Castle.Windsor;
using System.Reflection;
using System.IO;
using Castle.Windsor.Configuration.Interpreters;

namespace VietThinks.Model
{
    public class ServiceProvider
    {
        public readonly string DefaultWindsorConfigurationXmlFile;

        #region Singleton Representation

        static ServiceProvider()
        {
            Instance = new ServiceProvider();
        }

        public static ServiceProvider Instance { get; private set; }

        private ServiceProvider()
        {
            string assemblyUriLocation = Assembly.GetExecutingAssembly().CodeBase;
            string assemblyLocation = new Uri(assemblyUriLocation).LocalPath;
            DefaultWindsorConfigurationXmlFile = Path.Combine(
                Path.GetDirectoryName(assemblyLocation),
                "VietThinks.Model.Windsor.xml");

            this.container = new WindsorContainer(new XmlInterpreter(DefaultWindsorConfigurationXmlFile));
        }

        #endregion

        private readonly IWindsorContainer container;

        public TService GetService<TService>()
            where TService : class
        {
            return this.container.GetService<TService>();
        }
    }
}
